package dao

import (
	"go-web-scaffold/api"
	"go-web-scaffold/model"

	"log"
)

type UserDao struct {
	instance *api.SqliteDB
}

func NewUserDao(instance *api.SqliteDB) *UserDao {
	return &UserDao{
		instance,
	}
}

func (_this *UserDao) GetUser(userName string) (*model.User, error) {
	user := &model.User{}
	error := _this.instance.GetConnection().Raw("SELECT id,name,password FROM user WHERE name = ?", userName).Scan(user).Error
	if error != nil {
		log.Fatal("GetUser error")
		return nil, error
	}
	return user, nil
}

func (_this *UserDao) AddUser(userName string, password string) (*model.User, error) {
	user := &model.User{}
	error := _this.instance.GetConnection().Raw("INSERT INTO user(name,password) VALUES (?,?)", userName, password).Scan(user).Error
	if error != nil {
		log.Fatal("AddUser error")
		return nil, error
	}
	return user, nil
}
